<?php
class Hospital extends MY_Controller {

		function __construct()
		{
            parent::__construct();
			$this->load->library('myfunction');   //载入自定义函数库
			@date_default_timezone_set('Etc/GMT+8');  //设置默认时区为GMT+8
		}

		//医院列表
		function list_hospital() {
			$data['baseurl']=base_url();
			$page_num = '10';//每页的数据
			$this->load->model('hospital_model');
			$allhospital= $this->hospital_model->get_hospital_list($_SESSION['admin_info']['user_areacode'],0,$page_num,$this->uri->segment(4));//读出该分站的医院列表
			//print_r($data['article']);
			$total_nums=$this->hospital_model->get_hospital_num($_SESSION['admin_info']['user_areacode'],0);//读取该分站总医院数量
			$data['hospital']=$allhospital;
			$data['num']=$total_nums;
			$data['area']=$_SESSION['admin_info']['area'];
			$this->load->library('pagination');
			$config['base_url'] = base_url().'manage/hospital/list_hospital/';
			$config['uri_segment'] = 4;
			$config['total_rows'] = $total_nums;//总共多少条数据
			$config['per_page'] = $page_num;//每页显示几条数据
			$config['first_link'] = '首页';
			$config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
			$config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
			$config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
			$config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
			$config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
			$config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
			$config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
			$config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
			$config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
			$config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
			$config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
			$config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
			$config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
			$config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
			$config['num_tag_close'] = '</li>';
			$this->pagination->initialize($config);
			$this->load->view('manage/list_hospital_view',$data);
		}

		//添加医院
		function add_hospital() {
			$data['baseurl']=base_url();
			$data['title']='添加医院信息';
			$this->load->model('hospital_model');
			$data['rank']= $this->hospital_model->get_hsrank_info();//读取所有级别信息供列表输出
			$data['action']=base_url().'manage/hospital/do_add_hospital';
			$this->load->view('manage/add_hospital_view',$data);
		}

		//添加医院do
		function do_add_hospital() {
		$data['baseurl']=base_url();
		if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/hosimg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '800';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
				$this->do_image($imgname);//调用生成缩略图
			}else{
				$imgname='';
			}
			$hs['areacode']=$_SESSION['admin_info']['user_areacode'];//当前地区代码
			$hs['name']=$this->input->post('name');
			$hs['rank']=$this->input->post('rank');
			$hs['bednum']=$this->input->post('bednum');
			$hs['website']=$this->input->post('website');
			$hs['image']=$imgname;
			$hs['brief']=$this->myfunction->safe_convert($this->input->post('brief'));
			$hs['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
			$hs['vip']=$this->myfunction->safe_convert($this->input->post('vip'));
			$hs['address']=$this->input->post('address');
			$hs['contact']=$this->input->post('contact');
			$hs['busline']=$this->input->post('busline');
			$hs['puttime']=now();//当前时间（东8区）
			$this->load->model('hospital_model');
			$insertid=$this->hospital_model->add_hospital($hs);
			if($insertid!=1){ //如果插入记录成功
				if(($this->hospital_model->add_hs_id($_SESSION['admin_info']['user_areacode'],$insertid))==0){//返回插入医院ID（hs_id）
					$data['msg']='医院添加成功！';
					$data['rurl']=base_url().'manage/hospital/list_hospital';
					$this->load->view('manage/jsmsgbox_view',$data);
				}else{
					$data['msg']='医院添加失败，请与技术支持联系！';
					$data['rurl']=base_url().'manage/hospital/list_hospital';
					$this->load->view('manage/jsmsgbox_view',$data);
				}
			}
	   }

		//生成缩略图
		function do_image($imgname){
			$this->load->library('image_lib');
			$config['image_library'] = 'gd2';
			$config['source_image'] = "./uploads/hosimg/".$imgname;
			$config['new_image'] = "./uploads/hosimg/thumb/".$imgname;
			$config['width'] = 192;
			$config['height'] = 192;
			$this->image_lib->initialize($config);
			$this->image_lib->resize();
		}

		//编辑医院
		function edit_hospital() {
			$data['baseurl']=base_url();
			$data['title']='编辑医院信息';
			$hs_id=$this->uri->segment(4);
			if($this->ck_right('hs',$hs_id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$data['action']=base_url().'manage/hospital/do_edit_hospital/'.$hs_id;
				$this->load->model('hospital_model');
				$data['rank']= $this->hospital_model->get_hsrank_info();//读取所有级别信息供列表输出
				$hospital=$this->hospital_model->get_onehospital_info($hs_id);//获取医院信息
				$hospital['brief']=$this->myfunction->safe_invert($hospital['brief'],0);//去掉<br/>
				$hospital['intro']=$this->myfunction->safe_invert($hospital['intro'],0);//去掉<br/>
				$hospital['vip']=$this->myfunction->safe_invert($hospital['vip'],0);//去掉<br/>
				$data['hospital']=$hospital;
				$this->load->view('manage/edit_hospital_view',$data);
			}else{
				$data['msg']='无权操作，不能编辑此医院！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑医院do
		function do_edit_hospital() {
			$data['baseurl']=base_url();
			if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/hosimg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '800';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
				$this->do_image($imgname);//调用生成缩略图
			}else{
				$imgname='';
			}
			$hs_id=$this->uri->segment(4);
			$hs['name']=$this->input->post('name');
			$hs['rank']=$this->input->post('rank');
			$hs['bednum']=$this->input->post('bednum');
			$hs['website']=$this->input->post('website');
			$hs['image']=$imgname;
			$hs['brief']=$this->myfunction->safe_convert($this->input->post('brief'));
			$hs['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
			$hs['vip']=$this->myfunction->safe_convert($this->input->post('vip'));
			$hs['address']=$this->input->post('address');
			$hs['contact']=$this->input->post('contact');
			$hs['busline']=$this->input->post('busline');
			$hs['puttime']=now();//当前时间（东8区）
			$this->load->model('hospital_model');
			if(($this->hospital_model->edit_hospital($hs_id,$hs))==0){
				$data['msg']='医院信息修改成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='医院信息修改失败，请与技术支持联系！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//删除医院
		function do_del_hospital() {
			$hs_id=$this->uri->segment(4);
			if($this->ck_right('hs',$hs_id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('hospital_model');
				$this->hospital_model->del_hospital($hs_id); //删除医院
				$this->hospital_model->del_hos_dept($hs_id); //删除医院下属科室
				$this->hospital_model->del_hos_doct($hs_id); //删除医院下属医生
				$data['msg']='医院删除成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='无权操作，医院删除失败！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

//-----------------------------------------------------------科室部分------------------------------------------------------------//
		//添加科室
		function add_dept() {
			$data['baseurl']=base_url();
			$data['title']='添加科室信息';
			$this->load->model('hospital_model');
			$data['hospital']= $this->hospital_model->get_hospital_list($_SESSION['admin_info']['user_areacode']);//读取医院列表信息
			$data['depttype']= $this->hospital_model->get_depttype_info();//读取所有科室类型信息供列表输出
			$data['action']=base_url().'manage/hospital/do_add_dept';
			$this->load->view('manage/add_dept_view',$data);
		}

		//添加科室do
		function do_add_dept() {
		$data['baseurl']=base_url();
		if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/hosimg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '800';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
				$this->do_image($imgname);//调用生成缩略图
			}else{
				$imgname='';
			}
			$dp['areacode']=$_SESSION['admin_info']['user_areacode'];//当前地区代码
			$dp['hs_id']=$this->input->post('hospital');
			$dp['depttype']=$this->input->post('depttype');
			$dp['name']=$this->input->post('name');
			$dp['image']=$imgname;
			$dp['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
			$dp['tel']=$this->input->post('tel');
			$dp['puttime']=now();//当前时间（东8区）
			$this->load->model('hospital_model');
			if($this->hospital_model->add_dept($dp)==0){ //如果插入记录成功
				$this->hospital_model->edit_num('dc_hospital',$dp['hs_id'],'dept_num','+1');//修改医院信息表中的科室数量（加一）
				$data['msg']='科室添加成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='科室添加失败，请与技术支持联系！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
	   }

	   //科室列表
		function list_dept() {
			$data['baseurl']=base_url();
			$page_num = '10';//每页的数据
			$this->load->model('hospital_model');
			$alldept= $this->hospital_model->get_dept_list($this->uri->segment(4),'ALL',$page_num,$this->uri->segment(5));//读出该分站的医院列表
			//print_r($data['article']);
			$total_nums=$this->hospital_model->get_dept_num($this->uri->segment(4));//读取该医院科室数量
			$data['dept']=$alldept;
			$data['num']=$total_nums;
			$hospital=$this->hospital_model->get_onehospital_info($this->uri->segment(4));
			$data['hospital']=$hospital['name'];
			$this->load->library('pagination');
			$config['base_url'] = base_url().'manage/hospital/list_dept/'.$this->uri->segment(4);
			$config['uri_segment'] = 5;
			$config['total_rows'] = $total_nums;//总共多少条数据
			$config['per_page'] = $page_num;//每页显示几条数据
			$config['first_link'] = '首页';
			$config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
			$config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
			$config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
			$config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
			$config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
			$config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
			$config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
			$config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
			$config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
			$config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
			$config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
			$config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
			$config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
			$config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
			$config['num_tag_close'] = '</li>';
			$this->pagination->initialize($config);
			$this->load->view('manage/list_dept_view',$data);

		}


		//编辑科室
		function edit_dept() {
			$data['baseurl']=base_url();
			$data['title']='编辑科室信息';
			$id=$this->uri->segment(4);
			if($this->ck_right('dp',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$data['action']=base_url().'manage/hospital/do_edit_dept/'.$id;
				$this->load->model('hospital_model');
				$dept= $this->hospital_model->get_onedept_info($id);//根据id读出该科室信息
				$dept['intro']=$this->myfunction->safe_invert($dept['intro'],0);//去掉<br/>
				$data['hospital']= $this->hospital_model->get_onehospital_info($dept['hs_id']);
				$data['depttype']= $this->hospital_model->get_depttype_info();//读取所有科室类型信息供列表输出
				$data['dept']=$dept;
				$this->load->view('manage/edit_dept_view',$data);
			}else{
				$data['msg']='无权操作，不能编辑此科室！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑科室do
		function do_edit_dept() {
			$id=$this->uri->segment(4);
			$data['baseurl']=base_url();
			if($this->input->post('fileisok')!='') {
					$config['upload_path'] = './uploads/hosimg';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size'] = '800';
					$config['encrypt_name']='TRUE';
					$this->load->library('upload', $config);
					$this->upload->do_upload();
					$data = array('upload_data' => $this->upload->data());  //上传文件
					$imgname=$data['upload_data']['file_name']; 
					$this->do_image($imgname);//调用生成缩略图
				}else{
					$imgname='';
				}
				$dp['depttype']=$this->input->post('depttype');
				$dp['name']=$this->input->post('name');
				$dp['image']=$imgname;
				$dp['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
				$dp['tel']=$this->input->post('tel');
				$dp['puttime']=now();//当前时间（东8区）
				$this->load->model('hospital_model');
				if($this->hospital_model->edit_dept($id,$dp)==0){ //如果修改记录成功
					$data['msg']='科室信息修改成功！';
					$data['rurl']=base_url().'manage/hospital/list_hospital';
					$this->load->view('manage/jsmsgbox_view',$data);
				}else{
					$data['msg']='科室信息修改失败，请与技术支持联系！';
					$data['rurl']=base_url().'manage/hospital/list_hospital';
					$this->load->view('manage/jsmsgbox_view',$data);
			}
	   }

	   	//删除科室
		function do_del_dept() {
			$id=$this->uri->segment(4);
			if($this->ck_right('dp',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('hospital_model');
				$dept=$this->hospital_model->get_onedept_info($id);
				$hs_id=$dept['hs_id'];//查询所属HSID，以更新所属医院中的科室数量
				$doctnum=$dept['doct_num']; //查询该科室下属医生数量，以更新医院信息表中医生数量
				$this->hospital_model->del_dept($id);//删除科室
				$this->hospital_model->del_dept_doct($id);//删除科室下属医生
				$this->hospital_model->edit_num('dc_hospital',$hs_id,'dept_num','-1');//修改医院信息表中的科室数量(减一)
				$this->hospital_model->edit_num('dc_hospital',$hs_id,'doct_num','-'.$doctnum);//修改医院信息表中的科室数量(减去该科室下属医生数量)
				$data['msg']='科室删除成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='无权操作，科室删除失败！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

//---------------------------------------------------医生部分--------------------------------------------------//	

		//添加医生
	    function add_doctor(){
			$data['baseurl']=base_url();
			$data['title']='添加医生';
			$data['count']=0;
			$this->load->model('hospital_model');
			$data['rank']= $this->hospital_model->get_dcrank_info();//获取医生级别信息列表供输出
			$data['hospital']= $this->hospital_model->get_hospital_name($_SESSION['admin_info']['user_areacode']);//读取所有医院名称信息
			$data['dept']= $this->hospital_model->get_dept_name($_SESSION['admin_info']['user_areacode']);//读取所有科室名称信息
			$data['action']=base_url().'manage/hospital/do_add_doctor';
			$this->load->view('manage/add_doctor_view.php',$data);
		}

		//添加医生do
		function do_add_doctor() {
			$data['baseurl']=base_url();
			$dc['areacode']=$_SESSION['admin_info']['user_areacode'];//当前地区代码
			$dc['hs_id']=$this->input->post('hospital');
			$dc['dp_id']=$this->input->post('dept');
			$dc['name']=$this->input->post('name');
			$dc['sex']=$this->input->post('sex');
			$dc['rank']=$this->input->post('rank');
			$dc['special']=$this->input->post('special');
			$dc['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
			$dc['worktime']=$this->input->post('worktime');
			$dc['puttime']=now();//当前时间（东8区）
			$this->load->model('hospital_model');
			if($this->hospital_model->add_doctor($dc)==0){ //如果插入记录成功
				$this->hospital_model->edit_num('dc_hospital',$dc['hs_id'],'doct_num','+1');//修改医院信息表中的医生数量（加一）
				$this->hospital_model->edit_num('dc_dept',$dc['dp_id'],'doct_num','+1');//修改所属科室信息中的医生数量（加一）
				$data['msg']='医生添加成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='医生添加失败，请与技术支持联系！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
	   }

		//医生列表
		function list_doctor() {
			$col=$this->uri->segment(4);
			$data['baseurl']=base_url();
			$page_num = '10';//每页的数据
			$this->load->model('hospital_model');
			$alldoctor= $this->hospital_model->get_doctor_list($col,$this->uri->segment(5),$page_num,$this->uri->segment(6));//读出医生列表
			$c=count($alldoctor)-1;
			for($i=0;$i<=$c;$i++) {//循环替换结果数组中的数值
				$dept=$this->hospital_model->get_onedept_info($alldoctor[$i]['dp_id']);
				$alldoctor[$i]['dp_id']=$dept['name'];//将dp_id替换成科室名称
				$rank=$this->hospital_model->get_dcrank_info($alldoctor[$i]['rank']);
				$alldoctor[$i]['rank']=$rank;//将级别替换成级别名称
				$alldoctor[$i]['special']=$this->myfunction->trimmed_title($alldoctor[$i]['special']);//截取医生业务特长字符串
			}
			$total_nums=$this->hospital_model->get_doctor_num($col,$this->uri->segment(5));//读取医生数量
			$data['doctor']=$alldoctor;
			if ($col == 'hs_id') {
				$hospital=$this->hospital_model->get_onehospital_info($this->uri->segment(5));
				$title=$hospital['name'].'共有'.$total_nums.'名医生';
			}
			if ($col == 'dp_id') {
				$dept=$this->hospital_model->get_onedept_info($this->uri->segment(5));
				$title=$dept['name'].'共有'.$total_nums.'名医生';
			}
			$data['title']=$title;
			$this->load->library('pagination');
			$config['base_url'] = base_url().'manage/hospital/list_doctor/'.$this->uri->segment(4).'/'.$this->uri->segment(5);
			$config['uri_segment'] = 6;
			$config['total_rows'] = $total_nums;//总共多少条数据
			$config['per_page'] = $page_num;//每页显示几条数据
			$config['first_link'] = '首页';
			$config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
			$config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
			$config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
			$config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
			$config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
			$config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
			$config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
			$config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
			$config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
			$config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
			$config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
			$config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
			$config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
			$config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
			$config['num_tag_close'] = '</li>';
			$this->pagination->initialize($config);
			$this->load->view('manage/list_doctor_view',$data);

		}

		//编辑医生
		function edit_doctor() {
			$data['baseurl']=base_url();
			$data['title']='编辑医生信息';
			$id=$this->uri->segment(4);
			if($this->ck_right('dc',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$data['action']=base_url().'manage/hospital/do_edit_doctor/'.$id;
				$this->load->model('hospital_model');
				$data['rank']= $this->hospital_model->get_dcrank_info();//获取医生级别信息列表供输出
				$doctor= $this->hospital_model->get_onedoctor_info($id);//根据id获取医生信息
				$doctor['intro']=$this->myfunction->safe_invert($doctor['intro'],0);//去掉<br/>
				$data['hospital']= $this->hospital_model->get_onehospital_info($doctor['hs_id']);//读取所属医院信息
				$data['dept']= $this->hospital_model->get_dept_list($doctor['hs_id']);//读取所属医院所有科室名称信息
				$data['doctor']=$doctor;
				$this->load->view('manage/edit_doctor_view',$data);
			}else{
				$data['msg']='无权操作，不能编辑此医生！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑医生do
		function do_edit_doctor() {
			$id=$this->uri->segment(4);
			$data['baseurl']=base_url();
			$dc['dp_id']=$this->input->post('dept');
			$dc['old_dp_id']=$this->input->post('old_dept');//隐藏域记录原始科室id
			$dc['name']=$this->input->post('name');
			$dc['sex']=$this->input->post('sex');
			$dc['rank']=$this->input->post('rank');
			$dc['special']=$this->input->post('special');
			$dc['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
			$dc['worktime']=$this->input->post('worktime');
			$dc['puttime']=now();//当前时间（东8区）
			$this->load->model('hospital_model');
			if($this->hospital_model->edit_doctor($id,$dc)==0){ //如果插入记录成功
				if ($dc['old_dp_id'] != $dc['dp_id']) {	//如果修改了所属科室
					$this->hospital_model->edit_num('dc_dept',$dc['old_dp_id'],'doct_num','-1');//修改原科室信息中的医生数量（减一）
					$this->hospital_model->edit_num('dc_dept',$dc['dp_id'],'doct_num','+1');//修改新科室信息中的医生数量（加一）
				}
				$data['msg']='医生信息修改成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='医生信息修改失败，请与技术支持联系！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
	   }

	   	//删除医生
		function do_del_doctor() {
			$id=$this->uri->segment(4);
			if($this->ck_right('dc',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('hospital_model');
				$doctor=$this->hospital_model->get_onedoctor_info($id);
				$hs_id=$doctor['hs_id'];//查询所属HSID，以更新所属医院中的医生数量
				$dp_id=$doctor['dp_id'];//查询所属科室ID，以更新所属科室中的医生数量
				$this->hospital_model->del_doctor($id);//删除医生
				$this->hospital_model->edit_num('dc_hospital',$hs_id,'doct_num','-1');//修改医院信息表中的医生数量（减一）
				$this->hospital_model->edit_num('dc_dept',$dp_id,'doct_num','-1');//修改科室信息表中的医生数量(减一)
				$data['msg']='医生删除成功！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='无权操作，医生删除失败！';
				$data['rurl']=base_url().'manage/hospital/list_hospital';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

//----------------------------------------------体检部分----------------------------------------------------//

		//添加体检中心
		function add_exam() {
			$data['baseurl']=base_url();
			$data['title']='添加体检中心';
			$this->load->model('hospital_model');
			$data['hospital']= $this->hospital_model->get_hospital_list($_SESSION['admin_info']['user_areacode']);//读取医院列表信息
			$data['action']=base_url().'manage/hospital/do_add_exam';
			$this->load->view('manage/add_exam_view',$data);
		}

		//添加体检中心do
		function do_add_exam() {
		$data['baseurl']=base_url();
		if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/examimg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '500';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
				$this->do_image($imgname);//调用生成缩略图
			}else{
				$imgname='';
			}
			$ex['areacode']=$_SESSION['admin_info']['user_areacode'];//当前地区代码
			$ex['hs_id']=$this->input->post('hospital');
			$ex['name']=$this->input->post('name');
			$ex['image']=$imgname;
			$ex['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
			$ex['vip']=$this->myfunction->safe_convert($this->input->post('vip'));
			$ex['contact']=$this->input->post('contact');
			$ex['puttime']=now();//当前时间（东8区）
			$this->load->model('hospital_model');
			if($this->hospital_model->add_exam($ex)==0){ //如果插入记录成功
				$data['msg']='体检中心添加成功！';
				$data['rurl']=base_url().'manage/hospital/list_exam';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='体检中心添加失败，请与技术支持联系！';
				$data['rurl']=base_url().'manage/hospital/list_exam';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
	   }

	   //体检中心列表
		function list_exam() {
			$data['baseurl']=base_url();
			$data['title']='体检中心列表';
			$data['area']=$_SESSION['admin_info']['area'];
			$page_num = '10';//每页的数据
			$this->load->model('hospital_model');
			$allexam= $this->hospital_model->get_exam_list($_SESSION['admin_info']['user_areacode'],$page_num,$this->uri->segment(4));//读出该分站的中心列表
			$c=count($allexam)-1;
			for($i=0;$i<=$c;$i++) {//循环替换结果数组中的数值
				$allexam[$i]['vip']=$this->myfunction->trimmed_title($allexam[$i]['vip'],25);//截取会员优惠信息字符串
			}
			$total_nums=$this->hospital_model->get_exam_num($_SESSION['admin_info']['user_areacode']);//读取该地区体检中心数量
			$data['exam']=$allexam;
			$data['num']=$total_nums;
			$this->load->library('pagination');
			$config['base_url'] = base_url().'manage/hospital/list_exam/';
			$config['uri_segment'] = 5;
			$config['total_rows'] = $total_nums;//总共多少条数据
			$config['per_page'] = $page_num;//每页显示几条数据
			$config['first_link'] = '首页';
			$config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
			$config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
			$config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
			$config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
			$config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
			$config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
			$config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
			$config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
			$config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
			$config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
			$config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
			$config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
			$config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
			$config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
			$config['num_tag_close'] = '</li>';
			$this->pagination->initialize($config);
			$this->load->view('manage/list_exam_view',$data);

		}

		//编辑体检中心
		function edit_exam() {
			$data['baseurl']=base_url();
			$data['title']='编辑体检中心信息';
			$id=$this->uri->segment(4);
			if($this->ck_right('ex',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$data['action']=base_url().'manage/hospital/do_edit_exam/'.$id;
				$this->load->model('hospital_model');
				$exam= $this->hospital_model->get_oneexam_info($id);//根据id读出该体检中心信息
				$exam['intro']=$this->myfunction->safe_invert($exam['intro'],0);//去掉<br/>
				$exam['vip']=$this->myfunction->safe_invert($exam['vip'],0);//去掉<br/>
				$data['hospital']= $this->hospital_model->get_hospital_list($_SESSION['admin_info']['user_areacode']);//读取医院列表信息
				$data['exam']=$exam;
				$this->load->view('manage/edit_exam_view',$data);
			}else{
				$data['msg']='无权操作，不能编辑此体检中心！';
				$data['rurl']=base_url().'manage/hospital/list_exam';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑体检中心do
		function do_edit_exam() {
			$id=$this->uri->segment(4);
			$data['baseurl']=base_url();
			if($this->input->post('fileisok')!='') {
					$config['upload_path'] = './uploads/examimg';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['max_size'] = '500';
					$config['encrypt_name']='TRUE';
					$this->load->library('upload', $config);
					$this->upload->do_upload();
					$data = array('upload_data' => $this->upload->data());  //上传文件
					$imgname=$data['upload_data']['file_name']; 
					$this->do_image($imgname);//调用生成缩略图
				}else{
					$imgname='';
				}
				$ex['hs_id']=$this->input->post('hospital');
				$ex['name']=$this->input->post('name');
				$ex['image']=$imgname;
				$ex['intro']=$this->myfunction->safe_convert($this->input->post('intro'));
				$ex['vip']=$this->myfunction->safe_convert($this->input->post('vip'));
				$ex['contact']=$this->input->post('contact');
				$ex['puttime']=now();//当前时间（东8区）
				$this->load->model('hospital_model');
				if($this->hospital_model->edit_exam($id,$ex)==0){ //如果修改记录成功
					$data['msg']='体检中心信息修改成功！';
					$data['rurl']=base_url().'manage/hospital/list_exam';
					$this->load->view('manage/jsmsgbox_view',$data);
				}else{
					$data['msg']='体检中心信息修改失败，请与技术支持联系！';
					$data['rurl']=base_url().'manage/hospital/list_exam';
					$this->load->view('manage/jsmsgbox_view',$data);
			}
	   }

	   	//删除体检中心
		function do_del_exam() {
			$id=$this->uri->segment(4);
			if($this->ck_right('ex',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('hospital_model');
				$this->hospital_model->del_exam($id);//删除体检中心
				$data['msg']='体检中心删除成功！';
				$data['rurl']=base_url().'manage/hospital/list_exam';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='无权操作，体检中心删除失败！';
				$data['rurl']=base_url().'manage/hospital/list_exam';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

	

}
?>